package com.webside.front.deputyindex.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.webside.front.common.model.BSeries;
import com.webside.front.common.model.Bdictionary;
import com.webside.front.common.model.PBanner;
import com.webside.front.common.model.VideoFromWeb;
import com.webside.front.common.service.BAdPositionService;
import com.webside.front.common.service.BSeriesService;
import com.webside.front.common.service.BVideoFromWebService;
import com.webside.front.common.service.BdictionaryService;
import com.webside.front.common.service.PBannerService;
import com.webside.front.listener.BaseData;
import com.webside.util.Common;

@Controller
@RequestMapping(value = "/deputy/")
public class DeputyIndexController {
	@Autowired
	BSeriesService bSeriesService;
	@Autowired
	BdictionaryService bdictionaryService;
	@Autowired
	PBannerService pBannerService;
	@Autowired
	BVideoFromWebService bVideoFromWebService;
	@Autowired
	BAdPositionService bAdPositionService;
	
	@RequestMapping(value = "deputyindex.html", method = { RequestMethod.GET, RequestMethod.POST })
	public String getVarityIndex(Model m , HttpServletRequest request,String channel){
		Long l1 = System.currentTimeMillis(); 
		/*
		 * 频道名称
		 */
		m.addAttribute("channel", channel);
		m.addAttribute("channelName", this.getChannelName(channel));
		
		/*
		 * 图片根路径
		 */
		m.addAttribute("baseImgPath",BaseData.img_base_path);
		
		
		/*
		 * 获取导航部分列表（版本更新不再需要）
		 
		m.addAttribute("hearderMap", this.getChannelType());*/
		
		/*
		 * 搜索关键字 
		 */
		m.addAttribute("keyWords",this.getSearchKeyWords());
		
		/*
		 * banner
		 */
		m.addAttribute("bannerList", this.getBanners(channel));
		
		/*
		 * dictionary
		 */
		//查字典表的地区
		Map<String, Object> dictionary = new HashMap<String, Object>();
		dictionary.put("type", "AREA");
		dictionary.put("sort", "sort");
		dictionary.put("pageSize", 4);
		List<Bdictionary> dictionaryAreaList = bdictionaryService.selectBdictionaryByCondition(dictionary);
		m.addAttribute("dictionaryAreaList",dictionaryAreaList);
		
		
		//查字典表的类型
		dictionary.put("type", "DRAMA_TYPE");
		dictionary.put("code", channel);
		dictionary.put("pageSize", 10);
		dictionary.put("orderType", "ASC");
		//System.out.println(dictionary);
		List<Bdictionary> dictionaryTypeList = bdictionaryService.selectBdictionaryByCondition(dictionary);
		System.out.println(dictionaryTypeList);
		m.addAttribute("dictionaryTypeList",dictionaryTypeList);

		//查字典表的明星
		dictionary.put("type", "STAR");
		dictionary.remove("code");
		List<Bdictionary> dictionaryStarList = bdictionaryService.selectBdictionaryByCondition(dictionary);
		m.addAttribute("dictionaryStarList",dictionaryStarList);
		
		//查字典表的kind
		Map<String, Object> dictionaryKind = new HashMap<String, Object>();
		dictionaryKind.put("type", "SERIES_KIND");
		dictionaryKind.put("code", channel);
		dictionaryKind.put("sort", "sort");
		dictionaryKind.put("orderType", "ASC");
		List<Bdictionary> dictionaryKindList = bdictionaryService.selectBdictionaryByCondition(dictionaryKind);
		
		//获取该频道的热播
		m.addAttribute("bSeriesListHot",this.getHotSeries(channel));
		
		//b_video_from_web(花絮预告等)
		m.addAttribute("webVideoList",this.getWebVideo(channel));
		
		
		/*
		 * 第一部分 
		 * 1.推荐等级为1  大图 1张
		 * 2.推荐等级为2  小图 6张
		 */
		if (dictionaryKindList.size()>0) {
			Bdictionary dictionaryFirst = dictionaryKindList.get(0);
			String name = dictionaryFirst.getName();
			String code = dictionaryFirst.getCode();
			
			BSeries kindSeries = new BSeries();
			kindSeries.setKind(code);
			kindSeries.setChannel(channel);
			kindSeries.setStatus("PUBLISHED");
			kindSeries.setOrderByColumn("create_time");
			kindSeries.setOrderByType("DESC");
			//推荐等级1部分
			kindSeries.setRecommendLevel("1");
			kindSeries.setPageSize(1);
			List<BSeries> seriesList1 = bSeriesService.selectBseriesByPojo(kindSeries);
			//推荐等级2部分
			kindSeries.setRecommendLevel("2");
			kindSeries.setPageSize(6);
			List<BSeries> seriesList2 = bSeriesService.selectBseriesByPojo(kindSeries);
		
			m.addAttribute("name", name);
			m.addAttribute("seriesList1",seriesList1);
			m.addAttribute("seriesList2",seriesList2);
			
			dictionaryKindList.remove(0);
		}
		
		/**
		 * 通栏广告
		 */
		m.addAttribute("bannerOne", bAdPositionService.selectPositionByCode("BANNERONE"));
		Map<String, List<BSeries>> kindSeriesMap = new HashMap<String, List<BSeries>>(); 
		for (int i = 0; i < dictionaryKindList.size(); i++) {
			String name = dictionaryKindList.get(i).getName();
			String code = dictionaryKindList.get(i).getCode();
			BSeries kindSeries = new BSeries();
			kindSeries.setKind(code);
			kindSeries.setChannel(channel);
			kindSeries.setStatus("PUBLISHED");
			kindSeries.setOrderByColumn("create_time");
			kindSeries.setOrderByType("DESC");
			kindSeries.setPageSize(12);
			List<BSeries> seriesList = bSeriesService.selectBseriesByPojo(kindSeries);
			kindSeriesMap.put(name, seriesList);
		}
		m.addAttribute("kindSeriesMap", kindSeriesMap);
		Long l2 = System.currentTimeMillis();
		System.out.println("所用时间为"+(l2-l1)+"毫秒");
		/*
		 * 合作伙伴
		 */
		m.addAttribute("partnerList", this.getPartners());
		
		return Common.BACKGROUND_PATH+"/front/deindex/deputyIndex";
	}
	
	/**
	 * 获取导航部分的频道和下面的类型列表(导航部分去掉这部分暂时没用到)
	 * @return
	 */
	private Map<String, List<Bdictionary>> getChannelType(){
		//1.在字典表取出来channel有几个
		Map<String, Object> channelMap = new HashMap<String, Object>();
		channelMap.put("type", "CHANNEL");
		List<Bdictionary> channelList = bdictionaryService.selectBdictionaryByCondition(channelMap);
		
		Map<String, Object> typeMap = new HashMap<String, Object>();
		typeMap.put("type", "DRAMA_TYPE");
		typeMap.put("pageSize", 7);
		Map<String, List<Bdictionary>> headerMap= new HashMap<String,List<Bdictionary>>();
		List<Bdictionary> typeList = new ArrayList<Bdictionary>();
		//遍历频道 作为条件获取对应的type
		for (Bdictionary bdictionary : channelList) {
			String key = bdictionary.getCode();
			typeMap.put("code", key);
			typeList = bdictionaryService.selectBdictionaryByCondition(typeMap);
			headerMap.put(bdictionary.getName(), typeList);
		}
		
		return headerMap;
	}
	
	/**
	 * 获取频道名称
	 */
	private String getChannelName(String channel){
		Map<String, Object> condition = new HashMap<String,Object>();
		condition.put("type", "channel");
		condition.put("code", channel);
		List<Bdictionary> list = bdictionaryService.selectBdictionaryByCondition(condition);
		String name = null;
		if (list.size()>0) {
			name = list.get(0).getName();
		}
		return name;
	}
	
	/**
	 * 获取搜寻关键字
	 */
	private List<String> getSearchKeyWords(){
		Map<String, Object> keyWords = new HashMap<String, Object>();
		keyWords.put("type", "SEARCH_KEYWORDS");
		keyWords.put("code","SEARCH_KEYWORDS");
		List<Bdictionary> list = bdictionaryService.selectBdictionaryByCondition(keyWords);
		List<String> keyWord = new ArrayList<String>();
		String[] str = new String[]{};
		for (Bdictionary b : list) {
			str = b.getName().split(",");
			List<String> l = Arrays.asList(str);
			keyWord.addAll(l);
		}
		return keyWord;
	}
	
	/**
	 * 获取banner轮播图
	 * 传入频道
	 * @param channel
	 * @return
	 */
	private List<PBanner> getBanners(String channel){
		Map<String, Object> bannerMap = new HashMap<String, Object>();
		bannerMap.put("target", "BANNER");
		bannerMap.put("orderColumn", "sort");
		bannerMap.put("orderType", "ASC");
		bannerMap.put("channel", channel);
		bannerMap.put("pageSize", 5);
		List<PBanner> bannerList = pBannerService.selectBannerByCondition(bannerMap);
		System.out.println("轮播图长度为"+bannerList.size());
		return bannerList;
	}
	
	/**
	 * 获取热播部分
	 */
	private List<BSeries> getHotSeries(String channel){
		//热片 1.电影 2.取6条 3.观看次数降序
		BSeries bSeries = new BSeries();
		bSeries.setChannel(channel);
		bSeries.setStatus("PUBLISHED");
		bSeries.setPageSize(6);
		bSeries.setOrderByColumn("create_time");
		bSeries.setOrderByType("DESC");
		List<BSeries> bSeriesListHot = bSeriesService.selectBseriesByPojo(bSeries);
		return bSeriesListHot;
	}
	
	/**
	 * 获取资源片
	 */
	private List<VideoFromWeb> getWebVideo(String channel){
		//1.已发布 2.MOVIE 3.PUBLISHED 4.取6个 5.推荐等级1
		Map<String, Object> webVideoMap = new HashMap<String, Object>();
		webVideoMap.put("status", "PUBLISHED");
		webVideoMap.put("channel", channel);
		webVideoMap.put("pageSize", 6);
		webVideoMap.put("recommendLevel", "1");
		webVideoMap.put("orderByColumn", "play_count");
		webVideoMap.put("orderByType", "DESC");
		List<VideoFromWeb> webVideoList = bVideoFromWebService.selectWebVideoByCondition(webVideoMap);
		System.out.println("测试长度为："+webVideoList.size());
		return webVideoList;
	}
	
	/**
	 * 获取合作伙伴
	 * @return
	 */
	private List<PBanner> getPartners(){
		Map<String, Object> partnerMap = new HashMap<String, Object>();
		partnerMap.put("target", "PARTNER");
		partnerMap.put("orderColumn", "sort");
		partnerMap.put("orderType", "ASC");
		partnerMap.put("pageSize", 12);
		List<PBanner> partnerList = pBannerService.selectBannerByCondition(partnerMap);
		return partnerList;
	}

}
